package com.sky.commoncontroller;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;
@Slf4j
@RestController
 @RequestMapping("/admin/common/upload")
public class aliossController {
    @Autowired
    AliOssUtil aliOssUtil;
       @PostMapping()
       public Result<String> upload(MultipartFile file) {
           log.info("文件上传开始：{}", file.getOriginalFilename());
           String originalFilename = file.getOriginalFilename();
           String extion = originalFilename.substring(originalFilename.lastIndexOf("."));
           String objectName = UUID.randomUUID().toString() + extion;
           String url = null;
           try {
               url = aliOssUtil.upload(file.getBytes(), objectName);
           } catch (IOException e) {
               e.printStackTrace();
               log.info("文件上传失败");
           }
           return Result.success(url);

       }


}
